﻿#region Using statementes
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RhythmComet.Physics;
using FMOD;
using RhythmComet.Services;
using RhythmComet.Particles;
using System.Collections.Generic;
using System;
using System.Threading;
#endregion

namespace RhythmComet
{
    public interface IInstructionsState : IGameState { }

    public sealed class InstructionsState : GameState, IInstructionsState
    {
        private Texture2D[] images;
        private SpriteFont verdana;
        private SpriteFont font;
        private string[] message;
        private string objective;
        private Texture2D background;
        private Vector2 backgroundPosition;

        public InstructionsState(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(IInstructionsState), this);
        }

        public override void Update(GameTime gameTime)
        {
#if !XBOX
            if (Input.WasPressed(Buttons.Back) || Input.WasPressed(Buttons.Start) ||
                Input.WasPressed(Buttons.A) || Input.WasPressed(Keys.Enter))
#else
            if (Input.WasPressed(Buttons.Back) || Input.WasPressed(Buttons.Start) ||
                Input.WasPressed(Buttons.A))
#endif
                GameManager.PopState();

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Vector2 imagePosition = new Vector2(200, 260);
            Vector2 messagePosition = new Vector2(250, 270);

            game.GraphicsDevice.Clear(Color.Black);
            game.SpriteBatch.Begin();

            game.SpriteBatch.Draw(background, backgroundPosition, Color.White);

            Vector2 position = new Vector2(game.SpriteBatch.GraphicsDevice.Viewport.Width / 2, 60);
            Vector2 offset = font.MeasureString("Instructions") / 2;
            game.SpriteBatch.DrawString(font, "Instructions", position - offset, Color.Red);

            objective = @"Hit the obstacles of the same color of the comet, avoid the others";
            position.Y += 40;
            offset = verdana.MeasureString(objective) / 2;
            game.SpriteBatch.DrawString(verdana, objective, position - offset, Color.Red);

            objective = @"If you catch them in sequence, more points you get";
            position.Y += 20;
            offset = verdana.MeasureString(objective) / 2;
            game.SpriteBatch.DrawString(verdana, objective, position - offset, Color.Red);
            
            objective = @"Move the comet using the digital or analogic button";
            position.Y += 20;
            offset = verdana.MeasureString(objective) / 2;
            game.SpriteBatch.DrawString(verdana, objective, position - offset, Color.Red);

            objective = @"Press the gamepad buttons to change the color";
            position.Y += 20;
            offset = verdana.MeasureString(objective) / 2;
            game.SpriteBatch.DrawString(verdana, objective, position - offset, Color.Red);

            for (int i = 0; i < images.Length - 1; i++)
            {
                game.SpriteBatch.Draw(images[i], imagePosition, null, Color.White, 0.0f, Vector2.Zero, 0.5f, SpriteEffects.None, 1);
                game.SpriteBatch.DrawString(verdana, message[i], messagePosition, Color.White);
                imagePosition.X += 100;
                messagePosition.X += 100;
            }
            position.Y += 200;
            offset = font.MeasureString("Back") / 2;
            game.SpriteBatch.DrawString(font, "Back", position - offset, Color.Red);
            game.SpriteBatch.End();

            base.Draw(gameTime);
        }

        protected override void LoadContent()
        {
            verdana = game.Content.Load<SpriteFont>(@"fonts/Verdana");
            font = game.Content.Load<SpriteFont>(@"fonts\GridExercise");

            background = game.Content.Load<Texture2D>("sprites/background2");
            backgroundPosition = new Vector2((game.GraphicsDevice.Viewport.Width / 2 - background.Width / 2),
                (game.GraphicsDevice.Viewport.Height / 2 - background.Height / 2));

            images = new Texture2D[5];
            images[0] = game.Content.Load<Texture2D>("sprites/xboxControllerButtonA");
            images[1] = game.Content.Load<Texture2D>("sprites/xboxControllerButtonB");
            images[2] = game.Content.Load<Texture2D>("sprites/xboxControllerButtonX");
            images[3] = game.Content.Load<Texture2D>("sprites/xboxControllerButtonY");

            message = new string[4] { "Green", "Red", "Blue", "Yellow" };
        }
    }
}
